<template>
	<view style="margin: auto 30rpx; margin-top: 50rpx;">
		<view class="row padbottom20 minbottom20 align">
			<view class="col0 font500 size30" style="width: 20%;">
				姓名
			</view>
			<u--input placeholder="请输入内容" border="bottom" v-model="name"></u--input>
		</view>
		<view class="row padbottom20 minbottom20 align">
			<view class="col0 font500 size30" style="width: 20%;">
				性别
			</view>
			<view style="width: 80%;" @click="show = true">
				<u--input placeholder="请选择性别" type="number" disabled="false" border="bottom"
					v-model="gender"></u--input>
			</view>
		</view>
		<view class="row padbottom20 minbottom20 align">
			<view class="col0 font500 size30" style="width: 20%;">
				身份证号
			</view>
			<u--input placeholder="请输入内容" border="bottom" v-model="id_member"></u--input>
		</view>
		<view class="column padbottom20 minbottom20">
			<view class="col0 font500 size30 minbottom20">
				身份证正面照
			</view>
			<view class="">
				<u-upload :fileList="massif_img" @afterRead="massifRead" @delete="deletePic" name="massif" multiple
					:maxCount="1"></u-upload>
			</view>
		</view>
		<view class="column padbottom20 minbottom20">
			<view class="col0 font500 size30 minbottom20">
				身份证反面照
			</view>
			<view class="">
				<u-upload :fileList="file_img" @afterRead="massifRead" @delete="deletePic" name="file" multiple
					:maxCount="1"></u-upload>
			</view>
		</view>
		<view class="mintop40 padtop40">
			<u-button shape="circle" color="linear-gradient( 136deg, #FFBC43 0%, #FFDA97 100%)"
				customStyle="color: #885400;font-weight: 600;" text="保存修改" @click="getAddUser"></u-button>
		</view>
		<u-picker :show="show" :columns="columns" @confirm="categoryConfirm" @cancel="show = false"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: '',
				name: '',
				gender: '',
				id_member: '',
				id_front: '',
				id_anti: '',
				front: '',
				anti: '',
				show: false,
				massif_img: [],
				file_img: [],
				columns: [
					['男', '女', '未知']
				],
			}
		},
		onLoad() {
			this.user_id = uni.getStorageSync('user_id');
			this.getUserInfo()
		},
		onShow() {
			//当前页面展示的时候他就执行该生命周期

			if (this.user_id) {
				//如果存在token

				//按正常逻辑来讲 登录成功 我们是不是需要向服务器获取用户的信息？
				//那么我们一起来获取用户信息
			} else {
				this.$com.toast('您还未登录，正在前往登录')
				setTimeout(() => {
					this.$jump.nav('/mypackage/sign')
				}, 1000)
			}
		},
		methods: {
			async getUserInfo() {
				let {
					code,
					msg,
					data
				} = await this.$com.fetchGet("AppletUser/GetUserInfo", {
					id: this.user_id,
				})
				if (code == 1) {
					this.name = data.name
					this.gender = data.gender == 0 ? '未知' : data.gender == 1 ? '男' : '女'
					this.id_member = data.id_member
					var arr = {}
					var arrs = {}
					if (data.id_front) {
						arr['url'] = data.id_front
						this.massif_img.push(arr)
					}
					if (data.id_anti) {
						arrs['url'] = data.id_anti
						this.file_img.push(arrs)
					}

				} else {
					console.log(msg);
				}
			},
			async getAddUser() {
				var strTemp = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
				if (!strTemp.test(this.id_member)) { //判断如果手机号为空，提示用户输入手机号
					uni.showToast({
						title: '请输入正确的身份证号',
						icon: 'none'
					})
					return
				}
				let {
					code,
					msg,
					data
				} = await this.$com.fetch("AppletUser/authentication", {
					id: this.user_id,
					name: this.name,
					gender: this.gender == '未知' ? 0 : this.gender == '男' ? 1 : 2,
					id_member: this.id_member,
					id_front: this.front ? this.front : this.massif_img.length != 0 ? this.massif_img[0].url :
						'',
					id_anti: this.anti ? this.anti : this.file_img.length != 0 ? this.file_img[0].url : ''
				})
				if (code == 1) {
					this.$com.toast(msg)

					setTimeout(() => {
						this.$jump.switch('/pages/my/my')
					}, 1000)
				} else {
					this.$com.toast(msg)
				}
			},
			categoryConfirm(e) {
				this.show = false
				this.gender = e.value[0]
			},
			// 删除地块图片
			deletePic(event) {
				this[`${event.name}_img`].splice(event.index, 1)
			},
			// 新增图片
			async massifRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`${event.name}_img`].length
				lists.map((item) => {

					this[`${event.name}_img`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})

				for (let i = 0; i < lists.length; i++) {
					console.log(lists[i].url);
					const result = await this.uploadFilePromise(lists[i].url, event.name)
					let item = this[`${event.name}_img`][fileListLen]
					this[`${event.name}_img`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, name) {
				// 假设你已经获取了文件对象file
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://tlw.ynlckjht.com/api/AppletUpload/specsupload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test',
							server: 1,
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
							var data = JSON.parse(res.data)
							if (data.code == 1) {
								if (name == 'massif') {
									this.front = data.data
								} else {
									this.anti = data.data
								}
							}

						}
					});
				})
			},
		}
	}
</script>

<style scoped lang="less">
</style>